package com.happe.foodeasy.winter.typemap;

import com.happe.foodeasy.constraint.annotation.NumberTypeMetadata;
import com.happe.foodeasy.winter.conf.parser.DatabaseTypeMapParseResult;

import java.lang.reflect.Field;

/**
 * This class is used to map java Number(int long and so on) type to corresponding database type.
 * Created by wubin on 2016-05-20.
 */
public class NumberTypeMapDeal extends TypeMapDeal {
    @Override
    public String retrieveMapType(Field field) {
        NumberTypeMetadata numberTypeMetadata = field.getAnnotation(NumberTypeMetadata.class);
        DatabaseTypeMapParseResult result = getDatabaseTypeMapParseResult();
        if (numberTypeMetadata == null) {
            return result.getNumberType(0);
        } else {
            int upperLimit = numberTypeMetadata.digitUpperLimit();
            int decimalLength = numberTypeMetadata.decimalLength();
            if (upperLimit == -1) {
                return result.getNumberType(0);
            }

            return result.getNumberType(upperLimit, decimalLength);
        }
    }
}
